export const getBase64Image = (img) => {
  function getBase64Image(img, width, height) {
    //width、height调用时传入具体像素值，控制大小 ,不传则默认图像大小
    let canvas = document.createElement("canvas");
    canvas.width = width ? width : img.width;
    canvas.height = height ? height : img.height;

    let ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
    let dataURL = canvas.toDataURL();
    return dataURL;
  }
  let image = new Image();
  image.crossOrigin = "";
  image.src = img;
  return new Promise((resolve, reject) => {
    image.onload = function () {
      resolve(getBase64Image(image)); //将base64传给done上传处理
    };
  });
}


export const getImageSize = (url) => {
  return new Promise((resolve, reject) => {
    let img = new Image();
    img.src = url;
    img.onload = () => {
      resolve({
        width: img.width,
        height: img.height
      })
    }
  })
}

export const getImageFileSize = (url) => {
  return new Promise((resolve, reject) => {
    fetch(url).then((res) => {
      return res.blob()
    }).then(blob => {
      resolve(blob.size)
    }).catch(err => {
      reject(err)
    })
  })
}
